package ha.web.server.database;

import java.text.SimpleDateFormat;
import java.util.Date;

public class UserStatistics {
	
	private final String LB = "<br>";
	private int numberOfTurn;
	private int bigestJump;
	private int userId;
	private boolean won;
	private boolean resigned;
	private String timeStart;
	private String timeEnd;
	private int totalMoves;
	private String allMoves;
	private String otherPlayer;
	private String name;
	private int otherBigestJump;
	private int otherNumberOfTurn;
	
	public UserStatistics(int userId,String name) {
		this.name= name; 
		numberOfTurn = 0;
		bigestJump = 0;
		this.userId = userId;
		won = false;
		resigned = false;
		totalMoves = 0;
		timeStart = null;
		timeEnd = null;
		allMoves = null;
	}
	
	public void setEndGame(boolean won,boolean resigned) {
		this.won  = won;
		this.resigned = resigned;
	}
	
	public boolean isWon() {
		return won;
	}
	
	public boolean isResigned() {
		return resigned;
	}
	
	public int getUserId() {
		return userId;
	}
	
	public int getNumberOfMove() {
		return numberOfTurn;
	}


	public int getBigestJump() {
		return bigestJump;
	}

	public void update(int bigestJump) {
		numberOfTurn++;
		if(bigestJump>this.bigestJump)
			this.bigestJump = bigestJump;
	}

	public String getTimeStart() {
		return timeStart;
	}

	public String getTimeEnd() {
		return timeEnd;
	}
	
	public int getTotalMoves() {
		return totalMoves;
	}
	
	public String getAllMoves() {
		return allMoves;
	}

	public void setEndGameData(String timeStart,String timeEnd,int totalMoves,
			String allMoves,String otherPlayer,int otherBigestJump) {
		this.timeStart = timeStart;
		this.timeEnd = timeEnd;
		this.totalMoves = totalMoves;
		this.allMoves = allMoves;
		this.otherPlayer = otherPlayer;
		otherNumberOfTurn = totalMoves = numberOfTurn;
		this.otherBigestJump = otherBigestJump;
	}
	
	public String getTmpStatistics(String otherPlayer,int otherBigestJump,int totalMoves) {
		String toRet = "Statistics current game"+LB;
		toRet += "Players: "+name+" VS. "+otherPlayer+LB;
		toRet += "The game start at: "+timeStart+LB;
		toRet += "The current time is: "+new SimpleDateFormat ("hh:mm:ss").format(new Date())+LB;
		toRet += "Your bigest jump So far: "+bigestJump+LB;
		toRet += otherPlayer+" bigest jump so far: "+otherBigestJump+LB;
		toRet += "Number of turn that you play so far: "+numberOfTurn+LB;
		toRet += "Number of turn that "+otherPlayer+" play so far: "+(totalMoves-numberOfTurn)+LB;
		toRet += "Total moves so far: "+totalMoves+LB;
		return toRet;
	}
	
	@Override
	public String toString() {
		String toRet = "Statistics for last game"+LB;
		toRet += "Players: "+name+" VS. "+otherPlayer+LB;
		toRet += "Winner: "+(won?name:otherPlayer)+LB;
		toRet += "Your statistics:"+LB;
		toRet += "Number of turn that you play: "+numberOfTurn+LB;
		toRet += "Your bigest jump: "+bigestJump+LB;
		toRet += "Your opponent statistics:"+LB;
		toRet += "Number of turn that "+otherPlayer+" play: "+otherNumberOfTurn+LB;
		toRet += otherPlayer+" bigest jump: "+otherBigestJump+LB;
		toRet += "General statistics:"+LB;
		toRet += "Game start at: "+timeStart+LB;
		toRet += "Game end at: "+timeEnd+LB;
		toRet += "Total moves"+totalMoves+LB;
		toRet += allMoves+LB;
		return toRet;
	}

	
}
